package com.example.binderdemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new MyBinder();
    }

    class MyBinder extends Binder {

        @Override
        protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags) throws RemoteException {
            if(code==0x11){
                data.enforceInterface("com.example");//验证Binder标识，不是必须的
                int num1 = data.readInt();
                int num2 = data.readInt();
                int sum = num1+num2;
                reply.writeInt(sum);
                return true;
            }
            return super.onTransact(code, data, reply, flags);
        }
    }
}
